package com.uzai.console.service.profit.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.entity.WechatUser;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.dto.profit.prirewardrule.PriRewardRuleBatchSaveDto;
import com.uzai.console.dto.profit.prirewardrule.PriRewardRuleIdDto;
import com.uzai.console.dto.profit.prirewardrule.PriRewardRuleInfoDto;
import com.uzai.console.dto.profit.prirewardrule.PriRewardRuleQuery;
import com.uzai.console.dto.user.wechatuser.WechatUserQuery;
import com.uzai.console.entity.PriRewardRule;
import com.uzai.console.mapper.InviteRewardRuleMapper;
import com.uzai.console.mapper.PriRewardRuleMapper;
import com.uzai.console.mapper.WechatUserMapper;
import com.uzai.console.service.profit.PriRewardRuleService;
import com.uzai.console.vo.profit.prirewardrule.PriRewardRuleInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/***
 * @author liuqi
 * @date 2021年09月01日 19:26
 */
@Service
public class PriRewardRuleServiceImpl implements PriRewardRuleService {

    @Autowired
    private PriRewardRuleMapper priRewardRuleMapper;
    @Autowired
    private InviteRewardRuleMapper inviteRewardRuleMapper;
    @Autowired
    private WechatUserMapper wechatUserMapper;

    /**
     * 分页查询定向规则列表
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param priRewardRuleQuery
     * @return null
     */
    public Page<PriRewardRuleInfoVo> findPriRewardRuleList(PriRewardRuleQuery priRewardRuleQuery){
        //新建返回到页面的LIST对象
        List<PriRewardRuleInfoVo> priRewardRuleInfoVoList = new ArrayList<>();

        //执行分页查询对象
        Page<PriRewardRule> page = new Page<>();
        priRewardRuleQuery.convert(page);
        List<PriRewardRule> priRewardRuleList = priRewardRuleMapper.findByList(page, priRewardRuleQuery);

        if(priRewardRuleList != null){
            for (PriRewardRule priRewardRule : priRewardRuleList){
                PriRewardRuleInfoVo priRewardRuleInfoVo = new PriRewardRuleInfoVo();
                BeanUtils.copyProperties(priRewardRule, priRewardRuleInfoVo);
                priRewardRuleInfoVoList.add(priRewardRuleInfoVo);
            }
        }

        //返回到页面分页对象
        Page<PriRewardRuleInfoVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(priRewardRuleInfoVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询定向规则详情
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param priRewardRuleIdDto
     * @return null
     */
    public PriRewardRuleInfoVo findPriRewardRuleInfo(PriRewardRuleIdDto priRewardRuleIdDto){
        if(priRewardRuleIdDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        PriRewardRule priRewardRule = priRewardRuleMapper.selectById(priRewardRuleIdDto.getId(), priRewardRuleIdDto.getMerId());

        if(priRewardRule == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该定向规则不存在");
        }

        PriRewardRuleInfoVo priRewardRuleInfoVo =  new PriRewardRuleInfoVo();

        BeanUtils.copyProperties(priRewardRule, priRewardRuleInfoVo);

        return priRewardRuleInfoVo;
    }

    /**
     * 添加定向规则详情
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param priRewardRuleInfoDto
     * @return null
     */
    public void addPriRewardRule(PriRewardRuleInfoDto priRewardRuleInfoDto){
        if(StringUtils.isBlank(priRewardRuleInfoDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "定向规则名称不能为空");
        }
        if(priRewardRuleInfoDto.getUserRatio() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "买家比例不能为空");
        }
        if(priRewardRuleInfoDto.getParentRatio() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "上级比例不能为空");
        }
        if(priRewardRuleInfoDto.getSlParentRatio() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "上上级比例不能为空");
        }
        if(priRewardRuleInfoDto.getAgentRatio() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "代理比例不能为空");
        }
        if(priRewardRuleInfoDto.getSlAgentRatio() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "上级代理比例不能为空");
        }
        if(priRewardRuleInfoDto.getPartnerRatio() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "合伙人比例不能为空");
        }

        PriRewardRule priRewardRule =  new PriRewardRule();
        BeanUtils.copyProperties(priRewardRuleInfoDto, priRewardRule);

        Integer now = DateUtil.getNowTime();
        priRewardRule.setId(IdWorker.getId());
        priRewardRule.setCreateTime(now);
        priRewardRule.setUpdateTime(now);
        priRewardRuleMapper.insert(priRewardRule);
    }

    /**
     * 修改定向规则详情
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param priRewardRuleInfoDto
     * @return null
     */
    public void updatePriRewardRule(PriRewardRuleInfoDto priRewardRuleInfoDto){

        if(priRewardRuleInfoDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }
        if(StringUtils.isBlank(priRewardRuleInfoDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "定向规则名称不能为空");
        }

        if(priRewardRuleInfoDto.getUserRatio() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "买家比例不能为空");
        }
        if(priRewardRuleInfoDto.getParentRatio() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "上级比例不能为空");
        }
        if(priRewardRuleInfoDto.getSlParentRatio() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "上上级比例不能为空");
        }
        if(priRewardRuleInfoDto.getAgentRatio() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "代理比例不能为空");
        }
        if(priRewardRuleInfoDto.getSlAgentRatio() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "上级代理比例不能为空");
        }
        if(priRewardRuleInfoDto.getPartnerRatio() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "合伙人比例不能为空");
        }

        PriRewardRule priRewardRule = priRewardRuleMapper.selectById(priRewardRuleInfoDto.getId(), priRewardRuleInfoDto.getMerId());

        if(priRewardRule == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "此定向规则不存在");
        }

        priRewardRule.setName(priRewardRuleInfoDto.getName());
        priRewardRule.setUserRatio(priRewardRuleInfoDto.getUserRatio());
        priRewardRule.setParentRatio(priRewardRuleInfoDto.getParentRatio());
        priRewardRule.setSlParentRatio(priRewardRuleInfoDto.getSlParentRatio());
        priRewardRule.setAgentRatio(priRewardRuleInfoDto.getAgentRatio());
        priRewardRule.setSlAgentRatio(priRewardRuleInfoDto.getSlAgentRatio());
        priRewardRule.setPartnerRatio(priRewardRuleInfoDto.getPartnerRatio());

        priRewardRule.setUpdateTime(DateUtil.getNowTime());

        priRewardRuleMapper.update(priRewardRule);

    }

    /**
     * 删除定向规则详情
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param priRewardRuleIdDto
     * @return null
     */
    public void deletePriRewardRule(PriRewardRuleIdDto priRewardRuleIdDto){
        if(priRewardRuleIdDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        //判断该定向规则是否已经在用户上配置
        //1-个人微信用户
        WechatUserQuery wechatUserQuery = new WechatUserQuery();
        wechatUserQuery.setMerId(priRewardRuleIdDto.getMerId());
        wechatUserQuery.setPriRewardRules(String.valueOf(priRewardRuleIdDto.getId()));
        Page<WechatUser> page = new Page<>();
        page.setSize(5);
        page.setCurrent(1);
        List<WechatUser> wechatUserList = wechatUserMapper.findByList(page, wechatUserQuery);
        if(wechatUserList != null && wechatUserList.size() > 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该定向规则已经在用户定向比例中配置，不能删除");
        }

        priRewardRuleMapper.deleteById(priRewardRuleIdDto.getId(), priRewardRuleIdDto.getMerId());
    }

    /**
     * 批量操作定向规则
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param priRewardRuleBatchSaveDto
     * @return null
     */
    @Transactional
    public void batchSavePriRewardRule(PriRewardRuleBatchSaveDto priRewardRuleBatchSaveDto){

        List<PriRewardRuleInfoDto> priRewardRuleInfoDtoList = priRewardRuleBatchSaveDto.getPriRewardRuleInfoDtoList();

        if(priRewardRuleInfoDtoList == null || priRewardRuleInfoDtoList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "提交数据为空");
        }

        for (PriRewardRuleInfoDto rewardRuleInfoDto : priRewardRuleInfoDtoList){

            rewardRuleInfoDto.setMerId(priRewardRuleBatchSaveDto.getMerId());

            if(rewardRuleInfoDto.getId() == null ){
                addPriRewardRule(rewardRuleInfoDto);
            }else{
                updatePriRewardRule(rewardRuleInfoDto);
            }
        }

    }

}
